{template 'common/header'}
<script>

    $(function(){
        $("#member_goods").focus();
        $(".all_goods").keyup(function(){
            var key_word = $(this).val();
                var url = "{php echo $this->createWebUrl('orders',array('op' => 'find_goods'))}";
                $.ajax({
                    type : "post",
                    url : url,
                    data : {'key_word' : key_word},
                    success : function(data){
                        var return_data = JSON.parse(data);
                        if(return_data.errorCode == 0){
                            var table_html = "<tr><th>编号</th><th>商品</th><th>单位</th><th>运费</th></tr>";
                            for(var i = 0 ; i < return_data.goods.length ; i++){
                                table_html += "<tr ondblclick='choose_goods("+return_data.goods[i]['id']+",\"all_goods\");'><th>"+(i+1)+"</th><th><button class='btn btn-primary' onclick='choose_goods("+return_data.goods[i]['id']+",\"all_goods\");'>"+return_data.goods[i]['name']+"</button></th><th>"+return_data.goods[i]['unit']+"</th><th>"+return_data.goods[i]['price']+"</th></tr>";
                            }
                            $("#all-goods").html(table_html);
                        }else{
                            alert(return_data.msg);
                        }
                    }
                });
        });
        $(".member-goods").keyup(function(){
            var key_word = $(this).val();
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'find_member_goods'))}";
            var order_id = "{$order_id}";
            $.ajax({
                type : "post",
                url : url,
                data : {'key_word' : key_word, 'order_id' : order_id},
                success : function(data){
                    var return_data = JSON.parse(data);
                    if(return_data.errorCode == 0){
                        var table_html = "<tr><th>编号</th><th>商品</th><th>单位</th><th>运费</th></tr>";
                        for(var i = 0 ; i < return_data.goods.length ; i++){
                            table_html += "<tr  ondblclick='choose_goods("+return_data.goods[i]['id']+",\"member_goods\");'><th>"+(i+1)+"</th><th><button class='btn btn-primary' onclick='choose_goods("+return_data.goods[i]['id']+",\"member_goods\");'>"+return_data.goods[i]['name']+"</button></th><th>"+return_data.goods[i]['unit']+"</th><th style='width: 150px;'><input type='text' value='"+return_data.goods[i]['price']+"'  class='form-control' onblur='change_price("+return_data.goods[i]['member_id']+",this)' /></th></tr>";
                        }
                        $("#member-goods").html(table_html);
                    }else{
                        alert(return_data.msg);
                    }
                }
            });
        });
    });

    function change_price(id,obj){
        var price = $(obj).val();
//        var member_id = "{$order['member_id']}";
        var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_price'))}";
        $.ajax({
            type : "post",
            url : url,
            data : {'id' : id, 'price' : price},
            success : function(data){
                var json = JSON.parse(data);
                if(json.errorCode != 0){
                    alert(json.msg);
                }
            }
        });
    }
    function choose_goods(id,val){
        if(id > 0){
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'choose_goods'))}";
            var order_id = "{$order['id']}";
            $.ajax({
                type : "post",
                url : url,
                data : {'goods_id' : id, 'order_id' : order_id},
                success : function(data){
                    var json = JSON.parse(data);
                    if(json.errorCode == 0){
                        var htm = "<tr>\n" +
                            "                            <th>编号</th>\n" +
                            "                            <th>商品</th>\n" +
                            "                            <th>单位</th>\n" +
                            "                            <th>运费</th>\n" +
                            "                        </tr>";
                        htm += json.data;
                        $("#choosed").html(json.data);
                        $("#"+val).val('');
                        $("#"+val).focus();
                    }else{
                        alert(json.msg);
                    }
                }
            });
        }
    }
    function del_cart(id){
        if(id > 0){
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'del_cart'))}";
            $.ajax({
                type : "post",
                url : url,
                data : {'id' : id},
                success : function(data){
                    if(data == 111){
                        $("#c_tr"+id).remove();
                    }
                }
            });
        }else{
            alert("选择不存在！");
        }
    }
</script>
<ul class="nav nav-tabs">
    <li class="active"><a href="{php echo $this->createWebUrl('orders', array('op'=>'index'))}">订单列表</a></li>
    <li ><a href="{php echo $this->createWebUrl('orders', array('op'=>'add'))}">新建订单</a></li>
    <li ><a href="{php echo $this->createWebUrl('orders', array('op'=>'system'))}">待付款手续费设置</a></li>
</ul>
<div>

    <!--<form action="" method="post" class="form-horizontal form" enctype="multipart/form-data" onsubmit="return validate(this);">-->
        <div class="col-xs-6">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="panel-title">常用商品</div>
                </div>
                <div class="panel-body">
                    <input type="text" placeholder="商品名/助记码" class="form-control member-goods" id="member_goods"  />
                    <br />
                    <table class="table" id="member-goods">
                        <tr>
                            <th>编号</th>
                            <th>商品</th>
                            <th>单位</th>
                            <th>运费</th>
                        </tr>
                        {loop $member_goods $member_good}
                            <tr ondblclick="choose_goods({$member_good['goods']['id']},'member_goods')">
                                <th>{$member_good['key']}</th>
                                <th>
                                    <button class="btn btn-primary" onclick="choose_goods({$member_good['goods']['id']},'member_goods')">{$member_good['goods']['name']}</button>
                                </th>
                                <th>{$member_good['goods']['unit']}</th>
                                <th style="width:150px;"><input onblur="change_price('{$member_good["id"]}',this);" type="text" value="{$member_good['price']}" class="form-control" /></th>
                            </tr>
                        {/loop}
                    </table>
                </div>
            </div>
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="panel-title">
                        <div class="row">
                        <div class="col-xs-12">已选商品
                                <input type='hidden' name='id' value="{$theone['id']}" />
                                <a href="{php echo $this->createWebUrl('orders',array('op' => 'goods_add','order_id' => $order_id, 'new' => 1))}">
                                    <button class="btn btn-default">&nbsp;&nbsp;&nbsp;提&nbsp;&nbsp;&nbsp;交&nbsp;&nbsp;&nbsp;</button>
                                </a>
                                <a href="{php echo $this->createWebUrl('goods',array('op' => 'add','url' => 'order_goods_add','order_id' => $order_id))}">
                                    <button class="btn btn-default">新增商品</button>

                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <table class="table" id="choosed">
                        <tr>
                            <th>编号</th>
                            <th>商品</th>
                            <th>单位</th>
                            <th>运费</th>
                        </tr>
                        {loop $choosed_goods $key=>$c_good}
                        <tr ondblclick="del_cart({$c_good['id']});" id="c_tr{$c_good['id']}">
                            <td>{$key}</td>
                            <td>
                                    {$c_good['goods']}
                            </td>
                            <td>{$c_good['unit']}</td>
                            <td>{$c_good['price']}</td>
                        </tr>
                        {/loop}
                    </table>
                    <!--<div class="form-group col-sm-12">-->
                        <!--<input type='hidden' name='id' value="{$theone['id']}" />-->
                        <!--<a href="{php echo $this->createWebUrl('orders',array('op' => 'goods_add','order_id' => $order_id, 'new' => 1))}">-->
                            <!--<button class="btn btn-primary">提交</button>-->
                        <!--</a>-->
                        <!--<a href="{php echo $this->createWebUrl('goods',array('op' => 'add','url' => 'order_goods_add','order_id' => $order_id))}">-->
                            <!--<button class="btn btn-default">新增商品</button>-->

                        <!--</a>-->
                    <!--</div>-->
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="panel-title">全部商品</div>
                </div>
                <div class="panel-body">
                    <input type="text" placeholder="商品名/助记码" class="form-control all_goods"  id="all_goods" />
                    <br />
                    <table class="table" id="all-goods">
                        <tr>
                            <th>编号</th>
                            <th>商品</th>
                            <th>单位</th>
                            <th>默认运费</th>
                        </tr>
                        {loop $goods $good}
                        <tr ondblclick="choose_goods({$good['id']},'all_goods');">
                            <th>{$good['key']}</th>
                            <th>
                                    <button class="btn btn-primary" onclick="choose_goods({$good['id']},'all_goods');">{$good['name']}</button>
                            </th>
                            <th>{$good['unit']}</th>
                            <th>{$good['price']}</th>
                        </tr>
                        {/loop}
                    </table>
                    <div>
                        {$pager}
                    </div>
                </div>
            </div>
        </div>

    <!--</form>-->

</div>
{template 'common/footer'}